package com.example.backend.controller;

import com.example.backend.common.ResultMessage;
import com.example.backend.entity.DingDan;
import com.example.backend.entity.ShenFen;
import com.example.backend.service.IDingDanService;
import com.example.backend.service.impl.ShenFenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dingdan")
public class DingDanController {
    @Autowired
    private IDingDanService dingDanService;
    @Autowired
    private ShenFenService shenFenService;

    @PostMapping
    public ResultMessage addDingDan(@RequestBody DingDan dingDan){
        if(dingDanService.addDingDan(dingDan)){
            return ResultMessage.success(dingDan,"新增成功");

        }
        return ResultMessage.error("新增失败");
    }

    @DeleteMapping("/{ddid}")
    public ResultMessage deleteDingDan(@PathVariable("ddid") Integer ddid){
        if(dingDanService.deleteDingDan(ddid)){
            return ResultMessage.success(null,"删除成功");
        }
        return ResultMessage.error("删除失败");
    }

    @PutMapping
    public ResultMessage updateDingDan(@RequestBody DingDan dingDan){
        System.err.println(dingDan);
        if(dingDanService.updateDingDan(dingDan)){
            return ResultMessage.success(dingDan,"更新成功");
        }
        return ResultMessage.error("更新失败");
    }


    @GetMapping
    public ResultMessage queryDingDan(){
       return ResultMessage.success(dingDanService.queryDingDan(),"查询成功");
    }

    @GetMapping("/{ddid}")
    public ResultMessage queryDingDanById(@PathVariable("ddid") Integer ddid){
        return ResultMessage.success(dingDanService.queryDingDanById(ddid),"查询成功");
    }

    @GetMapping("/users/{yhid}")
    public ResultMessage queryDingDanByYhId(@PathVariable("yhid") Integer yhid){
        return ResultMessage.success(dingDanService.queryDingDanByYhId(yhid),"查询成功");
    }

    /**
     * 根据订单表的用户id查找用户表的信息
     * */
    @PostMapping("/users")
    public ResultMessage queryUsersByYhids(@RequestBody List<Integer> yhids) {
        try {
            // 调用用户服务批量查询用户信息
            List<ShenFen> shenFenList = new ArrayList<>();
            for(Integer yhid : yhids){
                ShenFen shenFen = shenFenService.queryShenFenById(yhid);
                if (shenFen != null) {
                    shenFenList.add(shenFen);
                }
            }
            return ResultMessage.success(shenFenList, "批量查询用户成功");
        } catch (Exception e) {
            return ResultMessage.error("批量查询用户失败：" + e.getMessage());
        }
    }

    @GetMapping("/dd/{sjid}")
    public ResultMessage querydd(@PathVariable("sjid") Integer sjid){
        return ResultMessage.success(dingDanService.querydd(sjid),"查询成功");
    }

    @GetMapping("/all/{sjid}")
    public ResultMessage queryAllDingDanBySjid(@PathVariable("sjid") Integer sjid){
        return ResultMessage.success(dingDanService.queryAllDingDanBySjid(sjid),"查询成功");
    }

    @GetMapping("/daipeisong/{sjid}")
    public ResultMessage queryDaiPeiSongDingDanBySjid(@PathVariable("sjid") Integer sjid){
        return ResultMessage.success(dingDanService.queryDaiPeiSongDingDanBySjid(sjid),"查询成功");
    }

}
